package com.trmq.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/5  10:59
 * 描述:
 */

@Component
@RabbitListener(queues = {"redis-queue"})
public class MyRabbitListener {

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitHandler
    public void messageHandler(String msg){

        System.out.println("======获取MQ消息 更新redis======");

        JSONObject jsonObject = JSON.parseObject(msg);

        Map<String, Object> map=new HashMap<>();
        map.put("id",jsonObject.get("id").toString());
        map.put("userName",jsonObject.get("userName").toString());

        redisTemplate.opsForHash().put("userList",jsonObject.get("id").toString(),map);
    }

}
